<template>
  <PageWrapper dense>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Space>
          <a-button
            type="primary"
            :pre-icon="IconEnum.ADD"
            @click="handleAdd"
            v-auth="'shop:shopInfo:add'"
            >新增店铺信息</a-button
          >
        </Space>
      </template>
      <template #toolbar>
        <Space>
          <a-button
            :preIcon="IconEnum.EXPORT"
            color="warning"
            @click="downloadExcel(shopInfoExport, '店铺信息数据')"
            ghost
            v-auth="'shop:shopInfo:export'"
            >导出数据</a-button
          >
          <a-button :preIcon="IconEnum.EXPAND" @click="expandAll">展开全部</a-button>
          <a-button :preIcon="IconEnum.COLLAPSE" @click="collapseAll">折叠全部</a-button>
        </Space>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            stopButtonPropagation
            :actions="[
              {
                label: '修改',
                icon: IconEnum.EDIT,
                type: 'primary',
                ghost: true,
                auth: 'shop:shopInfo:edit',
                onClick: handleEdit.bind(null, record),
              },
              // {
              //   label: '删除',
              //   icon: IconEnum.DELETE,
              //   type: 'error',
              //   ghost: true,
              //   auth: 'shop:shopInfo:remove',
              //   popConfirm: {
              //     placement: 'left',
              //     title: '是否删除店铺信息[' + record.id + ']?',
              //     confirm: handleDelete.bind(null, record),
              //   },
              // },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <ManageModal @register="registerModal" @reload="reloadWithCallback" />
  </PageWrapper>
</template>

<script setup lang="ts">
  import { PageWrapper } from '@/components/Page';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { Space } from 'ant-design-vue';
  import { shopInfoList, shopInfoExport } from '@/api/shop/manage';
  import type { ShopInfoVO } from '@/api/shop/manage/model';
  import { downloadExcel } from '@/utils/file/download';
  import { useModal } from '@/components/Modal';
  import ManageModal from './ManageModal.vue';
  import { formSchems, columns } from './manage.data';
  import { listToTree, removeEmptyChildren } from '@/utils/helper/treeHelper';
  import { IconEnum } from '@/enums/appEnum';

  defineOptions({ name: 'ShopInfo' });

  const [registerTable, { reloadWithCallback, expandAll, collapseAll }] = useTable({
    api: shopInfoList,
    showIndexColumn: false,
    isTreeTable: true,
    pagination: false,
    rowKey: 'id',
    afterFetch(data: ShopInfoVO[]) {
      const ret = listToTree(data, { id: 'shopId', pid: 'parentShopId' });
      removeEmptyChildren(ret);
      return ret;
    },
    useSearchForm: true,
    formConfig: {
      schemas: formSchems,
      baseColProps: {
        span: 8,
      },
    },
    columns: columns,
    actionColumn: {
      width: 200,
      title: '操作',
      key: 'action',
      fixed: 'right',
    },
  });

  const [registerModal, { openModal }] = useModal();

  function handleEdit(record: Recordable) {
    openModal(true, { record, update: true });
  }

  function handleAdd() {
    openModal(true, { update: false });
  }
</script>

<style scoped></style>
